package com.cancer.common.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class Lang {
	static Lang lang = new Lang("config/i118/geekplus");
	private static final String DEFAULT_LANGUAGE = "default";
	private final static Map<String, Locale> MAPPING = new HashMap<>();
	private final static ThreadLocal<String> _curLanguage = new ThreadLocal<String>();
	private static final Logger logger = LoggerFactory.getLogger(Lang.class);

	public static String msg(String key, Object... objects) {
		return lang.i118msg(key, I18.EnumTplOrMsgType.MSG, objects);
	}

	private String tplmsg(String key, Object... objects) {
		return i118msg(key, I18.EnumTplOrMsgType.BEETLE_MSG, objects);
	}

	static {
		MAPPING.put("default", Locale.CHINA);
		MAPPING.put("CN", Locale.CHINA);
		MAPPING.put("EN", Locale.ENGLISH);
		MAPPING.put("JPN", Locale.JAPAN);
		MAPPING.put("CNT", Locale.TRADITIONAL_CHINESE);
	}

	public static String getCurLanguage() {
		return _curLanguage.get();
	}

	public static void setCurLanguage(String language) {
		_curLanguage.set(language);
	}

	public static void removeAll() {
		_curLanguage.remove();
	}

	String baseName;

	public Lang(String baseName) {
		this.baseName = baseName;
	}

	/**
	 *
	 * @param key
	 *            国际化key
	 * @param type
	 *            类型 （java标准类型、模版类型）
	 * @param objects
	 *            参数对象
	 * @return
	 */
	private String i118msg(String key, I18.EnumTplOrMsgType type, Object... objects) {
		// 优先从 threadlocal 获取语言
		String userLang = _curLanguage.get();
		if (StringUtils.isBlank(_curLanguage.get())) {
			try {
				// 在从登录用户中获取语言
				userLang = Util.getSessionUserLanguage();
			} catch (Exception e) {
				logger.info("用户没有登录");
			}
		}
		Locale locale = MAPPING.get(userLang);
		if (locale == null) {
			// 最后使用默认语言
			locale = MAPPING.get(DEFAULT_LANGUAGE);
		}
		return I18.msg(this.baseName, locale, type, key, objects);
	}

	public static class Util {
		/**
		 * 通过登录用户获取 语言
		 * 
		 * @return
		 */
		public static String getSessionUserLanguage() {
			String language = null;
			try {
				Session session = SecurityUtils.getSubject().getSession();
				if (session != null) {
					Object user = session.getAttribute("loginUser");
					Method getLanguage = user.getClass().getMethod("getLanguage");
					language = (String) getLanguage.invoke(user);
				}
			} catch (Exception e) {
				// logger.info("通过登录用户获取语言失败，可能是没有登录或者没有使用shrio");
			}
			return language;
		}

		/**
		 * 从前端请求中获取语言
		 * 
		 * @param request
		 * @return
		 */
		public static String getLanguageFromRequest(HttpServletRequest request) {
			// 请求参数优先
			String language = request.getParameter("curLanguage");
			if (StringUtils.isBlank(language)) {
				// 请求头次之
				language = request.getHeader("curLanguage");
			}
			if (StringUtils.isBlank(language)) {
				// cookie最低优先级
				language = CookieUtils.getCookieValue(request, "curLanguage");
			}
			return language;
		}
	}
}
